/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.Terminal;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.TerminalRepository;
import me.zhengjie.service.TerminalService;
import me.zhengjie.service.dto.TerminalDto;
import me.zhengjie.service.dto.TerminalQueryCriteria;
import me.zhengjie.service.mapstruct.TerminalMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wuhj
* @date 2023-09-16
**/
@Service
@RequiredArgsConstructor
public class TerminalServiceImpl implements TerminalService {

    private final TerminalRepository terminalRepository;
    private final TerminalMapper terminalMapper;

    @Override
    public PageResult<TerminalDto> queryAll(TerminalQueryCriteria criteria, Pageable pageable){
        Page<Terminal> page = terminalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(terminalMapper::toDto));
    }

    @Override
    public List<TerminalDto> queryAll(TerminalQueryCriteria criteria){
        return terminalMapper.toDto(terminalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TerminalDto findById(String id) {
        Terminal terminal = terminalRepository.findById(id).orElseGet(Terminal::new);
        ValidationUtil.isNull(terminal.getId(),"Terminal","id",id);
        return terminalMapper.toDto(terminal);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Terminal resources) {
        if(StringUtils.isBlank(resources.getId()))
            resources.setId(IdUtil.simpleUUID());
        terminalRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Terminal resources) {
        Terminal terminal = terminalRepository.findById(resources.getId()).orElseGet(Terminal::new);
        ValidationUtil.isNull( terminal.getId(),"Terminal","id",resources.getId());
        terminal.copy(resources);
        terminalRepository.save(terminal);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            terminalRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TerminalDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TerminalDto terminal : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", terminal.getName());
            map.put("IP地址", terminal.getIpAddr());
            map.put("MAC地址", terminal.getMacAddr());
            map.put("创建时间", terminal.getCreateTime());
            map.put("更新时间", terminal.getUpdateTime());
            map.put("连接状态", terminal.getStatus());
            map.put("备注", terminal.getMemo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}